package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.enterprise.QuestionEnyiy;
import com.sgb.kjwl.model.entity.enterprise.TransferDataEntity;
import com.sgb.kjwl.model.entity.enterprise.TransferProcessEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MakeOverDetailQuestionModel extends AndroidViewModel {

    public MutableLiveData<List<QuestionEnyiy>> mQuestionList = new MutableLiveData<>();
    public MutableLiveData<List<TransferProcessEntity>> mProcessList = new MutableLiveData<>();
    public MutableLiveData<List<TransferDataEntity>> mDataList = new MutableLiveData<>();
    public MutableLiveData<Boolean> mSuccess = new MutableLiveData<>();
    public MutableLiveData<Boolean> mFail = new MutableLiveData<>();
    public MutableLiveData<Boolean> mLoading = new MutableLiveData<>();

    public MakeOverDetailQuestionModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getCommonQuestion(String correlation) {
        mLoading.postValue(true);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getquestionlistdata(correlation).enqueue(new Callback<BaseEntity<List<QuestionEnyiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<QuestionEnyiy>>> call, Response<BaseEntity<List<QuestionEnyiy>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mQuestionList.postValue(response.body().getData());
                } else {
                    MToast.showToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<QuestionEnyiy>>> call, Throwable t) {
                mFail.postValue(true);
            }
        });

    }

    public void getTransferProcess(String correlation) {
        mLoading.postValue(true);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTransferProcess(correlation).enqueue(new Callback<BaseEntity<List<TransferProcessEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TransferProcessEntity>>> call, Response<BaseEntity<List<TransferProcessEntity>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mProcessList.postValue(response.body().getData());
                } else {
                    MToast.showToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TransferProcessEntity>>> call, Throwable t) {
                mFail.postValue(true);
            }
        });

    }

    public void getTransferData(String correlation) {
        mLoading.postValue(true);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTransferData(correlation).enqueue(new Callback<BaseEntity<List<TransferDataEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TransferDataEntity>>> call, Response<BaseEntity<List<TransferDataEntity>>> response) {
                mSuccess.postValue(true);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mDataList.postValue(response.body().getData());
                } else {
                    MToast.showToast(getApplication().getApplicationContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TransferDataEntity>>> call, Throwable t) {
                mFail.postValue(true);
            }
        });

    }
}
